﻿using ASmile.Com;
using System;

namespace ASmile.Web
{
    public abstract class LoginStateBase : ILoginState
    {
        /// <summary>
        /// 从数据库中异步获取所有未到期的token列表，并放入缓存(异步)
        /// </summary>
        public virtual void InitCache()
        { }

        public abstract string CreateToken(LoginUser user);

        /// <summary>
        /// 检查Token是否有效
        /// </summary>
        public virtual bool CheckToken(string token, out LoginUser user)
        {
            user = Get(token);
            if (user == null || user.ExpiresTime < DateTime.Now || user.NotBefore > DateTime.Now)
            {
                return false;
            }
            return true;
        }
        /// <summary>
        /// 获取 LoginUser 对象
        /// </summary>
        public abstract LoginUser Get(string token);
        /// <summary>
        /// 设置缓存,并返回Token
        /// </summary>
        public abstract string Set(LoginUser user);
        /// <summary>
        /// 移除缓存
        /// </summary>
        public abstract bool Remove(string token);
        /// <summary>
        /// 刷新token,得到新的toke
        /// </summary>
        public abstract string RefreshToken(string token, out LoginUser user);
    }
}
